package com.qf.controller;

import com.qf.convert.MyConvert;
import com.qf.pojo.User;
import com.qf.service.AdminService;
import com.qf.service.impl.AdminServiceImpl;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.ConvertUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.Map;

/**
 * 修改用户的后台
 */
@WebServlet("/updateUser")
public class UpdateUserServlet extends HttpServlet {


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //解决post提交中文乱码
        request.setCharacterEncoding("utf-8");
        //响应乱码解决
        response.setContentType("text/html;charset=utf-8");


        //接收参数:
        //参数很多的时候: request.getParameter(name属性名称)
        //Enurmation request.getParameterName():获取所有的属性名称

        //封装User数据
        User user =new User() ;
       // System.out.println(user);
        //获取所有的前端的Map集合
        Map<String, String[]> map = request.getParameterMap();
        //Apache提供的通用工具类:commons-beanUtils:
        //beanUtils工具类:
        //public static void populate(Object bean, Map properties):
        //500错误:
        //修改页面表单: 日期:String   后台数据库存储的日期Date
        //需要日期转换器  ConvertUtils:BeanUtils的转换器
        //public static void register(Converter converter, Class clazz)
        //注册转换器的方法:参数1:转换器的接口 ,参数2:转换成指定类型的Class的字节码文件对象
        ConvertUtils.register(new MyConvert(), Date.class);


        // ConvertUtils.register(); //自定义一个日期转换器
        try {
            BeanUtils.populate(user,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
      //  System.out.println(user);


        //调用业务接口
        AdminService adminService = new AdminServiceImpl() ;
        adminService.update(user) ;


        //请求转发到 ---->后台:findUserAll
        request.getRequestDispatcher("/findUserAll").forward(request,response);






    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request,response);
    }
}
